#!/usr/bin/python

#Copyright 2012 Elias Farhan
#
#This file is part of Squirrel.
#
#Squirrel is free software: you can redistribute it and/or modify it under the terms of the GNU General 
#Public License as published by the Free Software Foundation, either version 3 of the License, or (at your 
#option) any later version.
#
#Squirrel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#See the GNU General Public License for more details.
class State():
	def __init__(self, name, speed, frames, index):
		self.name = name
		self.speed = speed
		self.frames = frames
		self.index = index
		self.boxes = []

class Moving():
	def __init__(self):
		self.UP = 0
		self.DOWN = 0
		self.LEFT = 0
		self.RIGHT = 0
		self.currentImg = 0
		
		self.frames = 4
		self.dimension = 4
		
		self.state = []
		self.currentState = 0
	def next(self):
		if (self.LEFT and self.RIGHT and not self.UP and not self.DOWN):
			self.currentImg = self.currentImg
		elif (self.RIGHT ):
			if (self.currentImg >=self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] ):
				self.currentImg = self.state[self.currentState].index
			else :
				self.currentImg = self.state[self.currentState].index + \
					((self.currentImg + 1)%self.state[self.currentState].frames[0])
		elif (self.LEFT):
			if ((self.currentImg < self.state[self.currentState].index + \
				self.state[self.currentState].frames[0]) or \
				(self.currentImg  >= self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				self.state[self.currentState].frames[1])):
					
				self.currentImg = self.state[self.currentState].index + \
				self.state[self.currentState].frames[0]
			else:
				self.currentImg = self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				((self.currentImg + 1) %self.state[self.currentState].frames[1])
		elif (self.UP and self.DOWN):
			self.currentImg = self.currentImg
		elif (self.UP):
			if ((self.currentImg < self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				self.state[self.currentState].frames[1]) or \
				(self.currentImg  >= self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				self.state[self.currentState].frames[1] + \
				self.state[self.currentState].frames[2])):
					
				self.currentImg = self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				self.state[self.currentState].frames[1]
			else:
				self.currentImg = self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				self.state[self.currentState].frames[1] + \
				((self.currentImg + 1) %self.state[self.currentState].frames[2])
		elif (self.DOWN):
			if ((self.currentImg < self.state[self.currentState].index + \
			self.state[self.currentState].frames[0] + \
			self.state[self.currentState].frames[1] + \
			self.state[self.currentState].frames[2]) or \
			(self.currentImg  >= self.state[self.currentState].index + \
			self.state[self.currentState].frames[0] + \
			self.state[self.currentState].frames[1] + \
			self.state[self.currentState].frames[2] + \
			self.state[self.currentState].frames[3])):
				
				self.currentImg = self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				self.state[self.currentState].frames[1] + \
				self.state[self.currentState].frames[2]
			else:
				self.currentImg = self.state[self.currentState].index + \
				self.state[self.currentState].frames[0] + \
				self.state[self.currentState].frames[1] + \
				self.state[self.currentState].frames[2] + \
				(self.currentImg + 1) %self.state[self.currentState].frames[3]
		else:
			self.currentImg = self.currentImg
		return self.currentImg
		
	def setCurrent(self, current, isUp):
		if (not isUp):
			if (current == 1):
				self.RIGHT = 1
			elif (current == 2):
				self.LEFT = 1
			elif (current == 3):
				self.UP = 1
			elif (current == 4):
				self.DOWN = 1
		else :
			if (current == 1) :
				self.RIGHT = 0
			elif (current == 2):
				self.LEFT = 0
			elif (current == 3): 
				self.UP = 0
			elif (current == 4):
				self.DOWN = 0
	def getBoxes(self):
		return self.state[self.currentState].boxes
	def getSpeed(self):
		return (self.state[self.currentState].speed*(self.RIGHT-self.LEFT), \
			self.state[self.currentState].speed*(self.DOWN-self.UP))
	def addState(self, name, speed, frames, index):
		self.state.append(State(name, speed, frames, index))
	def addBox(self, box, state):
		self.state[state].boxes.append(box)
	def setState(self, state):
		self.currentState = state
	def getNumState(self):
		len(self.state)
		
